package com.alavente.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.alavente.forms.LoginForm;
import com.alavente.objects.Client;
import com.alavente.service.ClientManager;

public class LoginAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, 
			HttpServletResponse response ) throws Exception 
	{ 		
		LoginForm f = (LoginForm)form;
		String login = f.getLogin();
		String mdp = f.getMdp();
		
		ClientManager clientManager = new ClientManager();
		Client client = clientManager.authenticateClient(login, mdp);
		
		if (client != null) {
			request.getSession().setAttribute("client", client);
			request.getSession().setAttribute("isAuthenticated", Boolean.TRUE);
			return mapping.findForward("home");
		}
		else {
			request.setAttribute("error", "Nom d'utilisateur ou mot de passe invalide");
			return mapping.findForward("failure");
		}
			
	}
}
